#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* detectCycle(struct ListNode* head)
{
    struct ListNode* fast = NULL;
    struct ListNode* slow = NULL;
    fast = slow = head;
    while (fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (slow == fast)
        {
            struct ListNode* meet = fast;
            struct ListNode* phead = head;
            while (meet != phead)
            {
                phead = phead->next;
                meet = meet->next;
            }
            return meet;
        }
    }
    return NULL;
}